﻿using System;
using System.Data.Odbc;
using InformaticaDiary.Common;
using System.Collections.Generic;

namespace InformaticaDiary.InformaticaDiaryDAL
{

    public class DataAccessOdbc : IDataAccess
    {
        OdbcTransaction transaction;
        private readonly string ConnString;
        OdbcConnection con;
        private DataAccessOdbc()
        {
        }
        public DataAccessOdbc(string ConnStr)
        {
            if (!String.IsNullOrEmpty(ConnStr))
            {
                ConnString = ConnStr;
            }
            else
            {
                throw new Exception("Connection String cannot be empty");
            }
            try
            {
                con = new OdbcConnection(ConnString);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public long Maxid(string strprint)
        {
            long functionReturnValue = 0;
            try
            {
                con.Open();
                string str = null;
                OdbcDataReader rdr = null;
                str = "select max(IdNo) from DairyMaster where User = '" + strprint.Trim() + "' ";
                OdbcCommand cmd = new OdbcCommand(str, con);
                rdr = cmd.ExecuteReader();
                rdr.Read();
                functionReturnValue = (long)rdr.GetValue(0);
                rdr.Close();
                con.Close();
                return functionReturnValue;
            }
            catch (Exception ex)
            {
                con.Close();
                return (0);
            }
        }

        public long Maxid1()
        {
            long functionReturnValue = 0;
            try
            {
                con.Open();
                string str = null;
                OdbcDataReader rdr = null;
                str = "select max(CaseTypeId) from CaseMaster";
                OdbcCommand cmd = new OdbcCommand(str, con);
                rdr = cmd.ExecuteReader();
                rdr.Read();
                functionReturnValue = (long)rdr.GetValue(0);
                rdr.Close();
                con.Close();
                return functionReturnValue;
            }
            catch (Exception ex)
            {
                con.Close();
                return (0);
            }
        }

        public long Maxid2()
        {
            long functionReturnValue = 0;
            try
            {
                con.Open();
                string str = null;
                OdbcDataReader rdr = null;
                str = "select max(IdNo) from DairyMaster";
                OdbcCommand cmd = new OdbcCommand(str, con);
                rdr = cmd.ExecuteReader();
                rdr.Read();
                functionReturnValue = (long)rdr.GetValue(0);
                rdr.Close();
                con.Close();
                return functionReturnValue;
            }
            catch (Exception ex)
            {
                con.Close();
                return (0);
            }
        }
       
        public ExecutedResult Insert(long Id, CaseDetails objCD)
        {
            throw new NotImplementedException();
        }

        public ExecutedResult Update(long Id, CaseDetails objCD)
        {
            throw new NotImplementedException();
        }

        public ExecutedResult Delete(long Id)
        {
            ExecutedResult obj = new ExecutedResult();
            try
            {
                con.Open();
                string str = null;

                str = "delete from DairyMaster where IdNo = " + Id;
                OdbcCommand cmd = new OdbcCommand(str, con);
                cmd.ExecuteNonQuery();
                obj.ErrorNo = 0;
                obj.ErrorDesc = "Record Deleted";
                con.Close();

            }
            catch (Exception ex)
            {
                obj.ErrorNo = 1;
                obj.ErrorDesc = ex.Message;
                con.Close();
            }
            return obj;
        }
        public ExecutedResult Login(LoginRequest objLoginRequest)
        {
            ExecutedResult obj = new ExecutedResult();
            System.DateTime d1 = default(System.DateTime);
            d1 = DateTime.Now;
            if (d1 < Convert.ToDateTime("15/02/2014 00:00:00"))
            {
                con.Open();
                OdbcDataReader rdr = null;
                string str = "select UserName, Password from UserMaster where UserName = '" + objLoginRequest.UserName + "' and Password = '" + objLoginRequest.Password + "'";
                OdbcCommand cmd = new OdbcCommand(str, con);
                rdr = cmd.ExecuteReader();
                rdr.Read();
                if (rdr.HasRows)
                {
                    string user = rdr["UserName"].ToString();
                    string pwd = rdr["Password"].ToString();
                    if (user == objLoginRequest.UserName & pwd == objLoginRequest.Password)
                    {
                        obj.ErrorNo = 0;
                        obj.ErrorDesc = "";
                    }
                    else
                    {
                        obj.ErrorNo = 9;
                        obj.ErrorDesc = "Invalid UserName or Password.";
                    }
                }
                else
                {
                    obj.ErrorNo = 9;
                    obj.ErrorDesc = "Invalid UserName or Password.";
                }
                rdr.Close();
                con.Close();
            }
            else
            {
                obj.ErrorDesc = "Your Trial version is expired !...............";
                obj.ErrorNo = 10;
            }
            return obj;
        }


        public List<SearchResult> DoSearch(SearchValue objSearchValue)
        {
            throw new NotImplementedException();
        }


        public List<CaseType> GetCaseType(long casetypeid = 0)
        {
            throw new NotImplementedException();
        }


        public List<int> GetYear()
        {
            throw new NotImplementedException();
        }


        public CaseDetails GetCaseDetails(long CaseDetailsID)
        {
            throw new NotImplementedException();
        }
    }


}
